﻿Imports System.Data.SqlClient

Public Class frmCot_Detalle
    Private Codigos As New DataTable("Codigos")
    Private Nombres As New DataTable("Nombres")
    Private CadenasAutocompletado As New AutoCompleteStringCollection
    Private CadenasAutocompletado2 As New AutoCompleteStringCollection
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub
    Sub CargaDetalle()
        dgDetalle.Rows.Clear()
        dgDetalle.Rows.Add(20)

        Dim cnconn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnconn.ConnectionString = CitraConnection
            strSql = "SELECT * FROM Cot_Detalle WHERE IdGeneral=" & IdGeneral & " ORDER BY IdDetalle"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Dim I As Integer = 0

            Do While rdBuscar.Read()
                dgDetalle.Rows(I).Cells(0).Value = rdBuscar("Codigo")
                dgDetalle.Rows(I).Cells(1).Value = rdBuscar("Producto")
                dgDetalle.Rows(I).Cells(2).Value = rdBuscar("Cantidad")
                dgDetalle.Rows(I).Cells(3).Value = rdBuscar("PU")
                dgDetalle.Rows(I).Cells(4).Value = rdBuscar("SubTotal")
                dgDetalle.Rows(I).Cells(5).Value = rdBuscar("IVA")
                dgDetalle.Rows(I).Cells(6).Value = rdBuscar("Total")
                I = I + 1
            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try

        dgDetalle.Columns(2).DefaultCellStyle.Format = "N2"
        dgDetalle.Columns(3).DefaultCellStyle.Format = "N2"
        dgDetalle.Columns(4).DefaultCellStyle.Format = "N2"
        dgDetalle.Columns(5).DefaultCellStyle.Format = "N2"
        dgDetalle.Columns(6).DefaultCellStyle.Format = "N2"

        dgDetalle.Columns(4).ReadOnly = True
        dgDetalle.Columns(5).ReadOnly = True
        dgDetalle.Columns(6).ReadOnly = True
       
    End Sub

    Private Sub frmCot_Detalle_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Codigos.PrimaryKey = Nothing
        Nombres.PrimaryKey = Nothing

        Codigos.Clear()
        Codigos.Columns.Clear()
        Nombres.Clear()
        Nombres.Columns.Clear()
    End Sub

    Private Sub frmCot_Detalle_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        CargaDetalle()

        CargaCodigosProductos()
        Dim claves(0) As DataColumn
        claves(0) = Codigos.Columns("Codigo")
        Codigos.PrimaryKey = claves

        For Each Codigo In Codigos.Rows
            CadenasAutocompletado.Add(Codigo("Codigo"))
        Next

        'CargaNombresProductos()
        Dim Llaves(0) As DataColumn
        Llaves(0) = Nombres.Columns("Nombre")
        'NombresProductos.PrimaryKey = Llaves

        For Each Nombre In Nombres.Rows
            CadenasAutocompletado2.Add(Nombre("Nombre"))
        Next
    End Sub
    Sub CargaCodigosProductos()
        Codigos.Columns.Add("Codigo")
        Nombres.Columns.Add("Nombre")

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim strSql As String = ""
        strSql = "Select cCodigoP01,cNombreP01"
        strSql = strSql & " From MGW10005 WHERE cstatusp01=1"
        strSql = strSql & " order by cCodigoP01"

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader
        Do While rdBuscar.Read()
            Codigos.Rows.Add(rdBuscar("cCodigoP01").ToString.Trim)
            Nombres.Rows.Add(rdBuscar("cNombreP01").ToString.Trim)
        Loop
    End Sub
    Private Sub ControlEdicion(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dgDetalle.EditingControlShowing
        If dgDetalle.CurrentCell.ColumnIndex = 0 Then
            DirectCast(e.Control, TextBox).AutoCompleteMode = AutoCompleteMode.SuggestAppend
            DirectCast(e.Control, TextBox).AutoCompleteSource = AutoCompleteSource.CustomSource
            DirectCast(e.Control, TextBox).AutoCompleteCustomSource = CadenasAutocompletado
        End If
        If dgDetalle.CurrentCell.ColumnIndex = 1 Then
            DirectCast(e.Control, TextBox).AutoCompleteMode = AutoCompleteMode.SuggestAppend
            DirectCast(e.Control, TextBox).AutoCompleteSource = AutoCompleteSource.CustomSource
            DirectCast(e.Control, TextBox).AutoCompleteCustomSource = CadenasAutocompletado2
        End If

        If dgDetalle.CurrentCell.ColumnIndex > 1 Then
            DirectCast(e.Control, TextBox).AutoCompleteMode = Nothing
            'DirectCast(e.Control, TextBox).AutoCompleteSource = Nothing
            'DirectCast(e.Control, TextBox).AutoCompleteCustomSource = Nothing
        End If
    End Sub
    Private Sub FinDeEdicion(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dgDetalle.CellEndEdit
        'Añadir aqui las validaciones necesarias
        Try
            If dgDetalle.CurrentCell.ColumnIndex = 0 Then
                Dim Producto As String = BuscarStrDatoCitra("SELECT cNombreP01 FROM MGW10005 WHERE UPPER(cCodigoP01)='" & UCase(dgDetalle.CurrentRow.Cells(0).Value) & "'")
                dgDetalle.CurrentRow.Cells(1).Value = Producto
                dgDetalle.CurrentRow.Cells(2).Value = 0
                dgDetalle.CurrentRow.Cells(3).Value = 0
                dgDetalle.CurrentRow.Cells(4).Value = 0
                dgDetalle.CurrentRow.Cells(5).Value = 0
                dgDetalle.CurrentRow.Cells(6).Value = 0
            ElseIf dgDetalle.CurrentCell.ColumnIndex = 1 Then
                Dim Codigo As String = BuscarStrDatoCitra("SELECT cCodigoP01 FROM MGW10005 WHERE UPPER(cNombreP01)='" & UCase(dgDetalle.CurrentRow.Cells(1).Value) & "'")
                dgDetalle.CurrentRow.Cells(0).Value = Codigo
                dgDetalle.CurrentRow.Cells(2).Value = 0
                dgDetalle.CurrentRow.Cells(3).Value = 0
                dgDetalle.CurrentRow.Cells(4).Value = 0
                dgDetalle.CurrentRow.Cells(5).Value = 0
                dgDetalle.CurrentRow.Cells(6).Value = 0
            ElseIf dgDetalle.CurrentCell.ColumnIndex = 2 Then
                If dgDetalle.CurrentRow.Cells(2).Value = "" Then
                    dgDetalle.CurrentRow.Cells(2).Value = 0
                End If
                dgDetalle.CurrentRow.Cells(4).Value = dgDetalle.CurrentRow.Cells(2).Value * dgDetalle.CurrentRow.Cells(3).Value
                dgDetalle.CurrentRow.Cells(5).Value = dgDetalle.CurrentRow.Cells(4).Value * 0.16
                dgDetalle.CurrentRow.Cells(6).Value = dgDetalle.CurrentRow.Cells(5).Value + dgDetalle.CurrentRow.Cells(4).Value
            ElseIf dgDetalle.CurrentCell.ColumnIndex = 3 Then
                If dgDetalle.CurrentRow.Cells(3).Value = "" Then
                    dgDetalle.CurrentRow.Cells(3).Value = 0
                End If
                dgDetalle.CurrentRow.Cells(4).Value = dgDetalle.CurrentRow.Cells(2).Value * dgDetalle.CurrentRow.Cells(3).Value
                dgDetalle.CurrentRow.Cells(5).Value = dgDetalle.CurrentRow.Cells(4).Value * 0.16
                dgDetalle.CurrentRow.Cells(6).Value = dgDetalle.CurrentRow.Cells(5).Value + dgDetalle.CurrentRow.Cells(4).Value
            End If

            dgDetalle.Columns(2).DefaultCellStyle.Format = "N2"
            dgDetalle.Columns(3).DefaultCellStyle.Format = "N2"
            dgDetalle.Columns(4).DefaultCellStyle.Format = "N2"
            dgDetalle.Columns(5).DefaultCellStyle.Format = "N2"
            dgDetalle.Columns(6).DefaultCellStyle.Format = "N2"

        Catch ex As Exception
            '.... Accion en caso de excepcion
        End Try
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        Call SetSqlCitra("DELETE FROM Cot_Detalle WHERE IdGeneral=" & IdGeneral)
        Dim Detalle As New clsCot_Detalle
        Dim Subtotal As Double = 0
        Dim iva As Double = 0
        Dim Total As Double = 0

        For i = 0 To dgDetalle.Rows.Count - 1
            If dgDetalle.Rows(i).Cells(0).Value = "" Then
                Exit For
            End If
            Detalle.IdGeneral = IdGeneral
            Detalle.IdProducto = BuscarIntDatoCitra("SELECT cIdProdu01 FROM MGW10005 WHERE UPPER(cCodigoP01)='" & UCase(dgDetalle.Rows(i).Cells(0).Value) & "'")
            Detalle.Codigo = dgDetalle.Rows(i).Cells(0).Value
            Detalle.Producto = dgDetalle.Rows(i).Cells(1).Value
            Detalle.Cantidad = dgDetalle.Rows(i).Cells(2).Value
            Detalle.PU = dgDetalle.Rows(i).Cells(3).Value
            Detalle.SubTotal = dgDetalle.Rows(i).Cells(4).Value
            Subtotal = Subtotal + Detalle.SubTotal
            Detalle.IVA = dgDetalle.Rows(i).Cells(5).Value
            Detalle.Total = dgDetalle.Rows(i).Cells(6).Value
            Detalle.Insertar()
        Next
        iva = Subtotal * 0.16
        Total = Subtotal + iva

        Call SetSqlCitra("UPDATE Cot_Generales SET Subtotal=" & Subtotal & ", IVA=" & iva & ",Total=" & Total & " WHERE IdGeneral=" & IdGeneral)
        frmModuloCotizaciones.CargaCotizaciones()
        Dim Resp As Integer = MsgBox("¿Deseas adjuntar un archivo a la cotización?", MsgBoxStyle.YesNo, "Aviso")
        If Resp = 6 Then
            frmModuloCotizaciones.InicializaTimer()
            frmModuloCotizaciones.Timer1.Enabled = True
            Close()
        Else
            Close()
        End If
    End Sub
End Class